﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TruckWatch.Cloud.BusinessLogic;
using TruckWatch.Cloud.Models;

namespace Mvc30WebRole1.Controllers
{
    public partial class ServicesController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        #region Update Status

        [HttpPost]
        public ActionResult UpdateStatus(string id)
        {
            try
            {
                // Read Querystring
                DriverCredential credential = GetDriverCredentialFromQueryString(id);

                TripStatusNEF tripStatus = new TripStatusNEF();

                UpdateModel(tripStatus);

                //tripStatus.StatusMessage =Request.Form["StatusMessage"];
                //if (tripStatus.StatusMessage == null)
                //    throw new AppException("Status Message is required");
                //tripStatus.StatusReason = Request.Form["StatusReason"];

                // Validate
                this.ValidateModel(tripStatus);


                // Write to database
                ServiceBL.UpdateStatus(credential, tripStatus);

                // Return view
                ViewBag.UserId = credential.UserId;
                return View(tripStatus);

            }
            catch (Exception)
            {
                throw; //TODO: return error message.
            }
        }

        private DriverCredential GetDriverCredentialFromQueryString(string userId)
        {
            DriverCredential credential = new DriverCredential();
            credential.UserId = Request.Form["UserId"];
            credential.PassHash = Request.Form["PassHash"];
            return credential;
        }

        #endregion

        #region BrowserClient

        /// <summary>
        /// Note: if I call this just "Browser" it does not work on Windows Azure!
        /// </summary>
        /// <returns></returns>
        public ActionResult Browser3()
        {
            return View();
        }
        #endregion

        #region Test

        public ActionResult Test()
        {
            var result = GoogleMaps.GetLatLng("7004 Albacete Ln. Round Rock TX, 78681");

            string result2 = GoogleMaps.GetAddress(result);
            
            return View();
        }

        #endregion
    }
}
